package com.ittao.taopicture.infrastructure.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "cos.client")
@Data
public class CosClientConfig {

    /**
     * 域名
     */
    private String host;

    /**
     * secretId
     */
    private String secretId;

    /**
     * secretKey 密钥
     */
    private String secretKey;

    /**
     * 区域
     */
    private String region;

    /**
     * 存储桶名称
     */
    private String bucket;

    @Bean
    public COSClient cosClient() {
        // 创建基本的COS凭证
        BasicCOSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 设置存储桶所在的区域
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        // 设置请求协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 返回一个COSClient实例
        return new COSClient(cred, clientConfig);
    }
}
